import { setSessionItem, getSessionItem } from 'utils/token'
import { I18N } from '@/variables/storageVariables'

const state = {
  lang: getSessionItem(I18N) || 'zhHans'
  // lang: getSessionItem(I18N) || 'en'
}

const mutations = {
  SET_LANG(state, payload) {
    state.lang = payload.lang
    setSessionItem(payload.key, payload.lang)
  }
}

const actions = {
  setI18nLang({ commit }, lang) {
    return new Promise(res => {
      const tokenKey = I18N
      const payload = { key: tokenKey, lang: lang }
      commit('SET_LANG', payload)
      res()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
